<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
	<title>sort</title>
	<meta http-equiv="Content-language" content="zh-CN" />
	<style type="text/css" media="all">
		.table{
			width:60%;
			border: 1px solid red;
			border-collapse: collapse;
		}
		.table td{
			border: 1px solid red;
			height: 20px;
		}
	</style>
</head>
<body>
<div id="page">
	<table  id="table" class="table">
		<tbody>
			<tr>
				<td>1</td>
				<td>One</td>
				<td>dom.require</td>
			</tr>
			<tr id="2" >
				<td class="2">2</td>
				<td>Two</td>
				<td>ControlJS </td>
			</tr>
			<tr id="3" >
				<td class="3">3</td>
				<td>Three</td>
				<td>HeadJS</td>
			</tr>
			<tr id="4" >
				<td class="4">4</td>
				<td>Four</td>
				<td>LAB.js</td>
			</tr>
			<tr id="5" >
				<td class="5">5</td>
				<td>Five</td>
				<td>$script.js</td>
			</tr>
			<tr id="6" >
				<td class="6">6</td>
				<td>Six</td>
				<td>NBL.js</td>
			</tr>
		</tbody>
	</table>
</div><!-- Page end! -->
<script type="text/javascript">
//<![CDATA[
window.onload = function(){
	//绑定事件
	var addEvent = document.addEventListener ? function(el,type,callback){
		el.addEventListener( type, callback, !1 );
	} : function(el,type,callback){
		el.attachEvent( "on" + type, callback );
	}
	//移除事件
	var removeEvent = document.removeEventListener ? function(el,type,callback){
		el.removeEventListener( type, callback );
	} : function(el,type,callback){
		el.detachEvent( "on" + type, callback);
	}
	//精确获取样式
	var getStyle = document.defaultView ? function(el,style){
		return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
	} : function(el,style){
		style = style.replace(/\-(\w)/g, function($, $1){
			return $1.toUpperCase();
		});
		return el.currentStyle[style];
	}
	var dragManager = {
		clientY:0,
		draging:function(e){//mousemove时拖动行
			var dragObj = dragManager.dragObj;
			if(dragObj){
				e = e || event;
				if(window.getSelection){//w3c
					window.getSelection().removeAllRanges();
				}else  if(document.selection){
					document.selection.empty();//IE
				}
				var y = e.clientY;
				var down = y > dragManager.clientY;//是否向下移动
				var tr = document.elementFromPoint(e.clientX, y);
				if(tr && tr.nodeName == "TD"){
					tr = tr.parentNode;
					dragManager.clientY = y;
					if( dragObj !== tr){
						tr.parentNode.insertBefore(dragObj, (down ? tr.nextSibling : tr));
					}
				};
			}
		},
		dragStart:function(e){
			  e = e || event;
			  var target = e.target || e.srcElement;
			  if(target.nodeName === "TD"){
				  target = target.parentNode;
				  dragManager.dragObj = target;
				  if(!target.getAttribute("data-background")){
					  var background = getStyle(target,"background-color");
					  target.setAttribute("data-background",background)
				  }
				  //显示为可移动的状态
				  target.style.backgroundColor = "#ccc";
				  target.style.cursor = "move";
				  dragManager.clientY = e.clientY;
			  }
		},
		dragEnd:function(){
			var dragObj = dragManager.dragObj;
			if (dragObj) {
				dragObj.style.backgroundColor = dragObj.getAttribute("data-background");
				dragObj.style.cursor = "default";
				dragManager.dragObj = null;
				//   removeEvent(document,"mousemove",dragManager.draging);
				//   removeEvent(document,"mouseup",dragManager.dragEnd);
			}
		},
		main:function(el){
			addEvent(el,"mousedown",dragManager.dragStart);
			addEvent(document,"mousemove",dragManager.draging);
			addEvent(document,"mouseup",dragManager.dragEnd);
		}
	}
	var el = document.getElementById("table");
	dragManager.main(el);
}
//]]>
</script>
</body>
</html>
